package leetcode.code1289;

public class Solution {
	public int minFallingPathSum(int[][] grid) {
		int m = grid.length, n = grid[0].length;
		int[][] s = new int[m][n];
		for (int i = 0; i < n; i++) {
			s[0][i] = grid[0][i];
		}
		for (int i = 1; i < m; i++) {
			for (int j = 0; j < n; j++) {
				s[i][j] = Integer.MAX_VALUE;
				for (int k = 0; k < s.length; k++) {
					if (k != j) {
						s[i][j] = Math.min(s[i][j], s[i - 1][k]);
					}
				}
				s[i][j] += grid[i][j];
			}
		}
		int ans = Integer.MAX_VALUE;
		for (int i = 0; i < n; i++) {
			ans = Math.min(ans, s[m - 1][i]);
		}
		return ans;
	}
}
